using System;
namespace cssp
{
	public class ConsoleLogger : ILogger, IDisposable
	{
		LogLevel mLogLevel;
		ConsoleColor mDefaultColor;

		public ConsoleLogger()
		{
			mDefaultColor = Console.ForegroundColor;
			mLogLevel = LogLevel.Error;
		}

		~ConsoleLogger()
		{
			Console.ForegroundColor = mDefaultColor;
		}

		#region ILogger Members

		public LogLevel LogLevel
		{
			get { return mLogLevel; }
			set { mLogLevel = value; }
		}

		public void Write(LogLevel level, string msg)
		{
			if (mLogLevel >= level)
			{
				if (level == LogLevel.Error || level == LogLevel.Fatal)
					Console.ForegroundColor = ConsoleColor.Red;
				Console.Out.Write(msg);
			}
		}

		void SetColor(LogLevel level)
		{
			switch (level)
			{
				case LogLevel.Fatal:
				case LogLevel.Error:
					Console.ForegroundColor = ConsoleColor.Red;
					break;
				case LogLevel.Debug:
					Console.ForegroundColor = ConsoleColor.DarkGray;
					break;
				default:
					Console.ForegroundColor = mDefaultColor;
					break;
			}
		}

		public void WriteLine(LogLevel level, string msg)
		{
			if (mLogLevel >= level)
			{
				SetColor(level);
				Console.Out.WriteLine(msg);
			}
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			Console.ForegroundColor = mDefaultColor;
		}

		#endregion
	}
}
